#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <list>
#include <map>
#include <iomanip>
#include <stack>

#include "Json.h"
#include "lex_ana.h"
#include "syntax_ana.h"

using namespace std;

void test_Json()
{
    auto num = new Json(996.123);

    auto str_j = new Json("good");

    auto arr_json = new Json_Array();
    arr_json->add(new Json(125));
    arr_json->add(new Json(12.5));
    arr_json->add(new Json(1.25));
    arr_json->add(new Json(10.25));

    auto head = new Json_Object();
    head->insert("k1", num);
    head->insert("k2", str_j);
    head->insert("k3", arr_json);

    head->insert("null_key", new Json());
    head->insert("null_bool", new Json(false));

    cout << head->to_string() << endl;

    cout << "arr_json size=" << arr_json->size() << endl;
    cout << "head size=" << head->size() << endl;
    cout << "head empty=" << head->empty() << endl;

    delete head;
}

vector<string> test_lex_ana()
{
    ifstream ifs("./test1.json");

    stringstream ss;
    string s = " ";
    while (ifs >> s)
    {
        ss << s;
    }
    ss << " ";
    s = ss.str();

    cout << s << endl;

    Lexical_Analysis la;

    vector<string> tokens = la.analysis(s);
    for (auto t : tokens)
    {
        cout << t << "  ";
    }
    cout << endl;
    return tokens;
}

void test_syntax_ana(){
    vector<string> tokens = test_lex_ana();
    try{ 
        Json* head = json_parse(tokens );
        cout << "-----------------------\n";
        cout << head->to_string() << endl;
    }catch(const char* e){
        printf("err: %s\n", e);
    } 
}

void test_json_parse_string(){
    ifstream ifs("./test1.json");

    stringstream ss;
    string s = " ";
    while (ifs >> s)
    {
        ss << s;
    }
    ss << " ";
    s = ss.str();
    
    Json* js = Json::parse_string(s);
    cout << "++++++++++++++++++++++++++++++++\n";
    cout << js->to_string() << endl;
    delete js;
}

// g++ src\*.cpp -o xmain.exe && xmain.exe
// g++ src/*.cpp -o xmain.exe && ./xmain.exe
int main()
{
    // test_Json();
    // test_lex_ana();
    // test_syntax_ana();
    test_json_parse_string();

    
    
}
/*

*/